package org.example.string;

public class LongestPalindrome {
    /**
     * 找出数组中所有字符串的最长公共前缀
     * @param strs
     * @return
     */
    public String longestCommonPrefix(String[] strs) {
        String ret = strs[0];
        for(int i=1;i<strs.length; i++){
            ret = findCommonPrefix(ret,strs[i]);
        }
        return ret;
    }

    /**
     * 从前往后一一比较找出公共前缀
     * @param s1
     * @param s2
     * @return
     */
    public String findCommonPrefix(String s1,String s2){
        int index = 0;
        int n = s1.length()<s2.length()?s1.length():s2.length();
        for(int i=0;i<n; i++){
            if(s1.charAt(i) == s2.charAt(i)){
                index++;
            }else{
                break;
            }
        }
        return s1.substring(0,index);
    }
}
